package com.amiba.crm.admin.aspect;

/**
 * @author HZI.HUI
 * 2019/5/23 18:31
 */

import com.amiba.crm.admin.annotation.EnableSysLog;
import com.amiba.crm.admin.auth.core.util.SysLogBean;
import com.amiba.crm.admin.auth.core.util.SysLogUtils;
import com.amiba.crm.admin.common.event.SysLogEvent;
import com.amiba.crm.admin.common.util.SpringContextHolder;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Slf4j
@Aspect
public class SysLogAspact {
    @SneakyThrows
    @Around(value="@annotation(enableSysLog)")
    public Object around(ProceedingJoinPoint point, EnableSysLog enableSysLog) {
        String strClassName = point.getTarget().getClass().getName();
        String strMethodName = point.getSignature().getName();
        log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName);

        SysLogBean logVo = SysLogUtils.getSysLog();
        logVo.setTitle(enableSysLog.value());
        // 发送异步日志事件
        Long startTime = System.currentTimeMillis();
        Object obj = point.proceed();
        Long endTime = System.currentTimeMillis();
        logVo.setTime(endTime - startTime);
        SpringContextHolder.publishEvent(new SysLogEvent(logVo));
        return obj;
    }
}
